package com.example.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.dao.*;
import com.example.entity.Yuanxi;
import com.example.exception.CustomException;
import com.example.service.YuanxiService;
import com.example.vo.YuanxiVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

@Service("yuanxiService")
public class YuanxiServiceImpl extends ServiceImpl<YuanxiDao, Yuanxi> implements YuanxiService {

    @Resource
    private YuanxiDao yuanxiDao;

    public Yuanxi add(Yuanxi yuanxi) {
		
	   //youquchong1
        yuanxi.setAddtime(new Date());
        yuanxiDao.insert(yuanxi);
        return yuanxi;
    }
	
	
	
    public void delete(Long id) {
        yuanxiDao.deleteById(id);
    }
	

    public void update(Yuanxi yuanxi) {
        //youquchong2
        yuanxiDao.updateById(yuanxi);
    }
	
	@Override
    public List<Map<String, Object>> daochuexcel() {
        return yuanxiDao.daochuexcel();
    }

    @Override
    public Yuanxi findById(Integer id) {
        return yuanxiDao.selectById(id);
    }

    public YuanxiVo findPage(YuanxiVo yuanxiVo) {
        LambdaQueryWrapper<Yuanxi> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtils.isNotEmpty(yuanxiVo.getYuanximingcheng()), Yuanxi::getYuanximingcheng,yuanxiVo.getYuanximingcheng());        
        Page<Yuanxi> page = new Page<>(yuanxiVo.getCurrent(),yuanxiVo.getCurrentNum());
        yuanxiDao.selectPage(page,wrapper);
        List<Yuanxi> yuanxis = page.getRecords();
        yuanxiVo.setList(yuanxis);
        yuanxiVo.setPages(page.getPages());
        yuanxiVo.setTotal(page.getTotal());
        yuanxiVo.setCurrent(page.getCurrent());
        yuanxiVo.setCurrentNum(page.getSize());
        return yuanxiVo;
    }

    @Override
    public void deleteList(List<Yuanxi> list) {
        List<Integer> ids =  list.stream().map(Yuanxi::getId).collect(Collectors.toList());
        yuanxiDao.deleteBatchIds(ids);
    }
	
	
	
	
	
	//youxxianxci
}
